<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/2000/svg"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	xmlns:dg="livcos.org/ns/doc/diagram"
	xmlns:mdl="livcos.org/ns/model"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:f="livcos.org/ns/xslt/function"
	exclude-result-prefixes="dg f xs mdl xlink">
	
	<xsl:import href="../../../../base.xsl"/>
	
	<xsl:param name="detailsView" select="'/livcos.org/web/doc'"/>

	<xsl:template match="mdl:type">
		<xsl:variable name="w" select="string-length(@name) * 10 + 20"/>
		<xsl:variable name="h" select="count(mdl:property) * 14 + 24"/>
		<xsl:variable name="c" select="$w div 2, $h div 2"/>
		<dg:box width="{$w}" height="{$h}" ref="{f:entity-id(@id)}">
			<defs>
				<marker id="mdl_arrow_inherit" refX="10" refY="5" markerUnits="strokeWidth"
					markerWidth="10" markerHeight="10" orient="auto">
					<path d="M 0,0 L 10,5 0,10 z" fill="white" stroke="black" stroke-width="1"/>
				</marker>
			</defs>
			<g fill="#CCDDEE">
				<a xlink:href="{f:caller-uri(concat($detailsView, f:object-id(.), '.html#', @id))}">
					<rect stroke="#001122" x="0" y="0" width="{$w}" height="{$h}" rx="1" ry="1">
						<title><xsl:value-of select="@name"/></title>
						<desc><xsl:value-of select="@name"/></desc>
					</rect>
					<text x="{$c[1]}" y="16" fill="black" style="text-anchor: middle; font-size: 12px" id="{f:entity-id(@id)}">
						<xsl:value-of select="@name"/>
					</text>
				</a>
				<xsl:if test="mdl:property">
					<line x1="0" y1="22" x2="{$w}" y2="22" stroke="#556677" stroke-width="1px"/>
					<rect x="1" y="22" width="{$w - 2}" height="{$h - 23}" fill="#EFF7FF"/>
					<g transform="translate(10,18)">
						<xsl:apply-templates select="mdl:property"/>
					</g>
				</xsl:if>
			</g>
			<xsl:apply-templates select="@specialize"/>
			<xsl:apply-templates select="mdl:relation"/>
		</dg:box>
	</xsl:template>
	
	<xsl:template match="mdl:type/@specialize">
		<dg:connect ref="{f:object-id(.)}#inherit_{../@id}" end-ref="{f:abs-ref(.)}" marker-end="#mdl_arrow_inherit"/>
	</xsl:template>
	
	<xsl:template match="mdl:relation">
	</xsl:template>
	
	<xsl:template match="mdl:relation[mdl:end/@ref]">
		<dg:connect ref="{f:entity-id(@id)}" end-ref="{f:abs-ref(mdl:end/@ref)}">
			<xsl:if test="not(mdl:start/@role)">
				<xsl:attribute name="marker-end" select="'#arrow_end'"/>
			</xsl:if>
			<xsl:apply-templates select="mdl:start/@* | mdl:end/@*"/>
		</dg:connect>
	</xsl:template>
	
	<xsl:template match="mdl:start/@* | mdl:end/@*">
	</xsl:template>
	
	<xsl:template match="mdl:start/@role" priority="2">
		<dg:text anchor="start" side="left"><xsl:value-of select="."/></dg:text>
	</xsl:template>
	
	<xsl:template match="mdl:end/@role" priority="2">
		<dg:text anchor="end" side="left"><xsl:value-of select="."/></dg:text>
	</xsl:template>
	
	<xsl:template match="mdl:start/@cardinality" priority="2">
		<dg:text anchor="start" side="right"><xsl:value-of select="."/></dg:text>
	</xsl:template>
	
	<xsl:template match="mdl:end/@cardinality" priority="2">
		<dg:text anchor="end" side="right"><xsl:value-of select="."/></dg:text>
	</xsl:template>
	
	<xsl:template match="mdl:property">
		<text x="0" y="{position() * 14}" fill="black" style="font-size: 10px" id="{f:entity-id(@id)}">
			<xsl:value-of select="@name"/>
			<xsl:if test="@type">: <xsl:value-of select="@type"/></xsl:if>
		</text>
	</xsl:template>
	
</xsl:stylesheet>